package com.gba.mark.user.auth.config;


import com.google.gson.Gson;
import com.gba.mark.user.auth.bean.AjaxResponseBody;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AjaxAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
        AjaxResponseBody responseBody = new AjaxResponseBody();

        responseBody.setStatus("400");
        responseBody.setMsg("Login Failure!");
        System.out.println("login fail");
        httpServletResponse.setStatus(400);
        httpServletResponse.getWriter().write(new Gson().toJson(responseBody));
        httpServletResponse.setHeader("refresh", "0;url=/user/login");

    }
}
